package xddq

import (
	"fmt"
	"runtime/debug"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

func (xd *XdDriver) FightInit() {
	xd.UserData.FightInfo.MaoXianAttr = make(chan int, 10)
	xd.UserData.FightInfo.ZhenYaoTaAttr = make(chan int, 10)
	xd.UserData.FightInfo.ZhenHuoMiJingAttr = make(chan int, 10)
	xd.UserData.FightInfo.ChallengeCoupon = make(chan int, 10)
	xd.UserData.FightInfo.SkyWar = make(chan D.SkyWarInfo, 10)
	xd.UserData.FightInfo.AskWay = make(chan D.AskWayInfo, 10)
	xd.UserData.Xianju.MapD = XianjuMapData
	xd.UserData.FightInfo.OfferCount = 0
}

// 更换当前装备抗性
func (xd *XdDriver) ChangeUserAttr(attr int) {
	data := ""
	switch attr {
	case D.Equipment_Attribute_Stun:
		data = "08954E" //击晕
		break
	case D.Equipment_Attribute_Critical_Strike:
		data = "08944E" //暴击
		break
	case D.Equipment_Attribute_Combo_Sttack:
		data = "08974E" //连击
		break
	case D.Equipment_Attribute_Dodge:
		data = "08934E" //闪避
		break
	case D.Equipment_Attribute_Counter_Attack:
		data = "08964E" //反击
		break
	case D.Equipment_Attribute_Blood_Suck:
		data = "08924E" //吸血
		break
	default:
		return
	}
	if xd.UserData.CurrentAttr != attr {
		log.Infof("当前属性[%s], 切换至[%s]", D.GetAttrDesc(xd.UserData.CurrentAttr), D.GetAttrDesc(attr))
		xd.UserData.CurrentAttr = attr
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_CLOUD_EQUIP, data))
	}
}

// 战斗任务
func (xd *XdDriver) StartFightTask() {
	log.Debug("start StartFightTask thread.")
	defer func() {
		if err := recover(); err != nil {
			log.Fatalf("process:\n%s", debug.Stack())
			xd.Close()
			time.Sleep(time.Second)
		}
	}()
	ZhenYaoTaFightCount := -1
	//xd.UserData.FightInfo.ChallengeCoupon <- 1 //斗法挑战
	for {
		select {
		case <-xd.ctx.Done():
			{
				log.Warnf("StartFightTask process thread exit.")
				return
			}
			//冒险挑战
		case attr := <-xd.UserData.FightInfo.MaoXianAttr:
			{
				needChangeAttr := false
				if attr >= 5 && attr <= 10 {
					if xd.UserData.CurrentAttr != 0 && xd.UserData.CurrentAttr != attr {
						needChangeAttr = true
					}
				}
				if needChangeAttr {
					xd.ChangeUserAttr(attr)
				}
				log.Infof("冒险-开始挑战")
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_STAGE_CHALLENGE, ""))
			}
			//镇妖塔挑战
		case attr := <-xd.UserData.FightInfo.ZhenYaoTaAttr:
			{
				//需要更换分身
				switchCloneFlag := false
				if xd.UserData.EquipmentCloneFlag && xd.UserData.CurrentEquipmentIdx != 2 {
					switchCloneFlag = true
					xd.SwitchCloneMu.Lock()
					xd.SwitchClone(2)
				}
				needChangeAttr := false
				if attr >= 5 && attr <= 10 {
					if xd.UserData.CurrentAttr != attr {
						needChangeAttr = true
					}
				}
				if needChangeAttr {
					xd.ChangeUserAttr(attr)
				}
				ZhenYaoTaFightCount++
				if ZhenYaoTaFightCount <= 0 {
					log.Infof("镇妖塔-一键选择技能")
				} else if ZhenYaoTaFightCount >= 2 {
					ZhenYaoTaFightCount = -1
				}
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_TOWER_SELECT_BUFF, "08001801"))
				log.Infof("镇妖塔-开始挑战")
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_TOWER_CHALLENGE, ""))
				if switchCloneFlag {
					xd.SwitchCloneMu.Unlock()
				}
			}
			//真火秘境挑战
		case attr := <-xd.UserData.FightInfo.ZhenHuoMiJingAttr:
			{
				needChangeAttr := false
				if attr >= 5 && attr <= 10 {
					if xd.UserData.CurrentAttr != attr {
						needChangeAttr = true
					}
				}
				if needChangeAttr {
					xd.ChangeUserAttr(attr)
				}
				log.Infof("真火秘境-开始挑战")
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_SECRETTOWER_FIGHT, "0801"))
			}
			/*
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  193]:   ↑↑↑↑↑ S_RANK_BATTLE_GET_BATTLE_LIST - len:18

				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  200]:   PayloadBytes: 71 AB 00 00 00 12 00 00 4F BA 00 00 02D4 07 01 81 E2
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  195]:   ↓↓↓↓↓ game cmd[410]-unkown - len:360
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  200]:   PayloadBytes: 71 AB 00 00 01 68 00 00 01 9A 00 00 02 D4 07 01 81 E2 08 00 12 45 0A 3E 08 EE FF FF FF FF FF FF FF FF 01 10 B9 CA 08 1A12 E5 B7 AB E9 A9 AC E5 8D 88 E7 AB AF E5 B0 8A E8 80 85 20 09 28 99 81 06 38 B9 CA 08 40 A9 E6 BD 01 4A 00 50 00 5A 00 60 00 68 00 70 00 10 E8 07 18 00 12 42 0A 3D 08 F4 E3 CA B8 C0 5A 10 CB E5 08 1A 0F E9 9B A8 E8 BD A9 E3 80 81 E9 81 AEE5 A4 A9 20 D3 04 28 A6 B6 A2 9E 17 38 CC E5 08 40 AA E6 BD 01 4A 00 50 FA EC 08 60 EC F4 08 70 00 78 AE 4E 10 B8 10 12 42 0A 3D 08 8F A3 B0 B8 C0 5A 10 CC E5 08 1A 0F E9 9B A8 E8 BD A9 E4 B8 B6 E5 B0 8F E9 B1 BC 20 D3 04 28 DC F6 F8 BD 1838 CC E5 08 40 AA E6 BD 01 4A 00 50 FA EC 08 60 D6 F4 08 70 00 78 9E 4E 10 B3 11 12 42 0A 3D 08 E9 FA BC B8 C0 5A 10 DE C9 08 1A 0F E9 9B A8 E8 BD A9 EF BD 80 E7 8B 97 E8 9B 8B 20 89 05 28 E7 C0 92 C5 18 38 DE C9 08 40 AA E6 BD 01 4A 00 50FA EC 08 60 D6 F4 08 70 00 78 93 4E 10 D1 10 12 3D 0A 38 08 F8 FC EE B7 C0 5A 10 90 8C 09 1A 0C E6 98 9F E8 BE B0 E7 A0 8D E6 A0 91 20 89 05 28 A2 FE C8 A2 19 38 CD E5 08 40 A9 E6 BD 01 4A 00 50 00 60 F9 F4 08 70 00 78 93 4E 10 AB 1018 00
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 1 - code 08[8] len:0-_
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 2 - code 12[18] len:69-0A 3E 08EE FF FF FF FF FF FF FF FF 01 10 B9 CA 08 1A 12 E5 B7 AB E9 A9 AC E5 8D 88 E7 AB AF E5 B0 8A E8 80 85 20 09 28 99 81 06 38 B9 CA 08 40 A9 E6 BD 01 4A 00 50 00 5A 00 60 00 68 00 70 00 10 E8 07 18 00_

				����������巫马午端尊者 	(��8�@���J P Z ` h p �
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 3 - code 12[18] len:66-0A 3D 08F4 E3 CA B8 C0 5A 10 CB E5 08 1A 0F E9 9B A8 E8 BD A9 E3 80 81 E9 81 AE E5 A4 A9 20 D3 04 28 A6 B6 A2 9E 17 38 CC E5 08 40 AA E6 BD 01 4A 00 50 FA EC 08 60 EC F4 08 70 00 78 AE 4E 10 B8 10_

				��ʸ�Z�雨轩、遮天 �(����8�@���J P�`�p x�N�
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 4 - code 12[18] len:66-0A 3D 088F A3 B0 B8 C0 5A 10 CC E5 08 1A 0F E9 9B A8 E8 BD A9 E4 B8 B6 E5 B0 8F E9 B1 BC 20 D3 04 28 DC F6 F8 BD 18 38 CC E5 08 40 AA E6 BD 01 4A 00 50 FA EC 08 60 D6 F4 08 70 00 78 9E 4E 10 B3 11_

				�����Z�雨轩丶小鱼 �(����8�@���J P�`�p x�N�
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 5 - code 12[18] len:66-0A 3D 08E9 FA BC B8 C0 5A 10 DE C9 08 1A 0F E9 9B A8 E8 BD A9 EF BD 80 E7 8B 97 E8 9B 8B 20 89 05 28 E7 C0 92 C5 18 38 DE C9 08 40 AA E6 BD 01 4A 00 50 FA EC 08 60 D6 F4 08 70 00 78 93 4E 10 D1 10_

				�����Z�雨轩｀狗蛋 �(����8�@���J P�`�p x�N�
				[DEBUG] [2024/10/04 10:25:10] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 6 - code 12[18] len:61-0A 38 08F8 FC EE B7 C0 5A 10 90 8C 09 1A 0C E6 98 9F E8 BE B0 E7 A0 8D E6 A0 91 20 89 05 28 A2 FE C8 A2 19 38 CD E5 08 40 A9 E6 BD 01 4A 00 50 00 60 F9 F4 08 70 00 78 93 4E 10 AB 10_

				�����Z��	星辰砍树 �(��Ȣ8�@���J P `�p x�N�
			*///斗法挑战
		case <-xd.UserData.FightInfo.ChallengeCoupon:
			{
				xd.ResetTaskByID(proto.S_RANK_BATTLE_GET_BATTLE_LIST)
			}
			//征战诸天
		case skyWarInfo := <-xd.UserData.FightInfo.SkyWar:
			{
				if !skyWarInfo.Flag {
					time.Sleep(3 * time.Second)
					log.Infof("征战诸天-查看信息")
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_SKY_WAR_ENTER, "08"+xd.UserData.UserID))
				} else {
					selfCombatVal := xd.UserData.CurrentEquipmentInfo[2].CombatValue
					if skyWarInfo.FightCount > 0 {
						fightNum := -1
						minCombatValueNum := 0
						minCombatValue := skyWarInfo.People[1].CombatValue
						for idx, info := range skyWarInfo.People {
							//if info.CombatValue < xd.UserData.FightInfo.CombatValue {
							if (info.CombatValue/10000)*5 < (selfCombatVal/10000)*4 {
								if fightNum < 0 {
									fightNum = idx
								}
							}
							if info.CombatValue < minCombatValue {
								minCombatValue = info.CombatValue
								minCombatValueNum = idx
							}
						}
						//有多的刷新次数 但是第一名打不过则刷新
						if skyWarInfo.RefreshCount > 0 && skyWarInfo.RefreshCount >= skyWarInfo.FightCount && fightNum <= 2 {
							log.Infof("征战诸天-免费刷新，剩余刷新次数%d次", skyWarInfo.RefreshCount)
							xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_SKY_WAR_REFRESH_ENEMY, "08"+xd.UserData.UserID))
							skyWarInfo.RefreshCount--
							xd.UserData.FightInfo.SkyWar <- D.SkyWarInfo{Flag: false} //查看征战诸天
							continue
						}
						finalFight := -1
						if skyWarInfo.FightCount > 0 {
							if fightNum >= 0 {
								finalFight = fightNum //有能打过的人
							} else {
								finalFight = minCombatValueNum //没有能打过的人选战力最低的
							}
						}
						if finalFight >= 0 {
							fightPeople := skyWarInfo.People[finalFight]
							//需要更换分身
							switchCloneFlag := false
							if xd.UserData.EquipmentCloneFlag && xd.UserData.CurrentEquipmentIdx != 2 {
								switchCloneFlag = true
								xd.SwitchCloneMu.Lock()
								xd.SwitchClone(2)
							}
							log.Infof("征战诸天-开始挑战第【%d】位 %s, 战力%s, 剩余挑战次数%d次", finalFight+1, fightPeople.Name,
								D.GetCombatValueDesc(fightPeople.CombatValue), skyWarInfo.FightCount)
							xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_SKY_WAR_FIGHT,
								fmt.Sprintf("08 %s 10 %s 18 %s 20 %02d", xd.UserData.UserID, fightPeople.UserID, fightPeople.ID, finalFight)))
							if switchCloneFlag {
								xd.SwitchCloneMu.Unlock()
							}
							xd.UserData.FightInfo.SkyWar <- D.SkyWarInfo{Flag: false} //查看征战诸天
						}
					} else {
						xd.StopTaskByID(proto.S_SKY_WAR_ENTER)
						log.Infof("征战诸天-领取出战1次礼物")
						xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_TASK_GET_REWARD, "08 D1 E8 0C"))
						log.Infof("征战诸天-领取出战3次礼物")
						xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_TASK_GET_REWARD, "08 D2 E8 0C"))
						log.Infof("征战诸天-领取出战5次礼物")
						xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_TASK_GET_REWARD, "08 D3 E8 0C"))
					}
				}
			}
			//问道盛会
		case AskWayInfo := <-xd.UserData.FightInfo.AskWay:
			{
				if !AskWayInfo.Flag {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ASKWAY_ENTER, "08"+xd.UserData.Activity[D.Activity_Ask_Grand_Event].ID))
				} else {
					selfCombatVal := xd.UserData.CurrentEquipmentInfo[2].CombatValue
					if AskWayInfo.FightCount > 0 {
						fightNum := -1
						minCombatValueNum := 0
						minCombatValue := AskWayInfo.People[1].CombatValue
						for idx, info := range AskWayInfo.People {
							//if info.CombatValue < xd.UserData.FightInfo.CombatValue {
							if (info.CombatValue/10000)*5 < (selfCombatVal/10000)*4 {
								if fightNum < 0 {
									fightNum = idx
								}
							}
							if info.CombatValue < minCombatValue {
								minCombatValue = info.CombatValue
								minCombatValueNum = idx
							}
						}
						//有多的刷新次数 但是第一名打不过则刷新
						if AskWayInfo.RefreshCount > 0 && AskWayInfo.RefreshCount >= AskWayInfo.FightCount && fightNum <= 2 {
							log.Infof("问道盛会-没有妖力低于%s的道友，开始免费刷新，剩余刷新次数%d次", D.GetCombatValueDesc(selfCombatVal/10*8), AskWayInfo.RefreshCount)
							xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ASKWAY_MATCH, fmt.Sprintf("08 %s 10 %02X", xd.UserData.Activity[D.Activity_Ask_Grand_Event].ID, 10-AskWayInfo.RefreshCount)))
							AskWayInfo.RefreshCount--
							xd.UserData.FightInfo.AskWay <- D.AskWayInfo{Flag: false} //查看问道盛会
							continue
						}
						finalFight := -1
						if AskWayInfo.FightCount > 0 {
							if fightNum >= 0 {
								finalFight = fightNum //有能打过的人
							} else {
								finalFight = minCombatValueNum //没有能打过的人选战力最低的
							}
						}
						if finalFight >= 0 {
							fightPeople := AskWayInfo.People[finalFight]
							//需要更换分身
							switchCloneFlag := false
							if xd.UserData.EquipmentCloneFlag && xd.UserData.CurrentEquipmentIdx != 2 {
								switchCloneFlag = true
								xd.SwitchCloneMu.Lock()
								xd.SwitchClone(2)
							}
							log.Infof("问道盛会-分身妖力%s 开始挑战第【%d】位 %s, 战力%s, 剩余挑战次数%d次", D.GetCombatValueDesc(selfCombatVal),
								finalFight+1, fightPeople.Name, D.GetCombatValueDesc(fightPeople.CombatValue), AskWayInfo.FightCount)
							xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ASKWAY_BATTLE,
								fmt.Sprintf("08 %s 10 %s 18 %02X", xd.UserData.Activity[D.Activity_Ask_Grand_Event].ID, fightPeople.UserID, AskWayInfo.Group)))
							if switchCloneFlag {
								xd.SwitchCloneMu.Unlock()
							}
							xd.UserData.FightInfo.AskWay <- D.AskWayInfo{Flag: false} //查看问道盛会
						}
					} else {
						xd.StopTaskByID(proto.S_ASKWAY_ENTER)
					}
				}
			}
		}
	}
}
